package com.spring.pay.weixin.handler;


import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.spring.pay.alipay.service.impl.AlipayServiceImpl;
import com.spring.pay.weixin.config.WeiXinConfig;
import com.spring.pay.weixin.util.MD5Util;
import com.spring.pay.weixin.util.TenpayUtil;



/**
 * 应答处理类
 * 应答处理类继承此类，重写isTenpaySign方法即可。
 * @author miklchen
 *
 */
public class ResponseHandler { 
	private Logger LOGGER = Logger.getLogger(ResponseHandler.class);
	/** 密钥 */
	private String key;
	
/*	*//** 应答的参数 *//*
	private SortedMap parameters; */
	
	/** debug信息 */
	private String debugInfo;
	
	private HttpServletRequest request;
	
	private HttpServletResponse response;
	
	private String uriEncoding;
	
	/**
	 * 构造函数
	 * 
	 * @param request
	 * @param response
	 */
	public ResponseHandler(HttpServletRequest request,
			HttpServletResponse response)  {
		this.request = request;
		this.response = response;

		this.key = "";
		this.debugInfo = "";
		
		this.uriEncoding = "";

	}
	
	/**
	*获取密钥
	*/
	public String getKey() {
		return key;
	}

	/**
	*设置密钥
	*/
	public void setKey(String key) {
		this.key = key;
	}

/*	*//**
	 * 获取参数值
	 * @param parameter 参数名称
	 * @return String 
	 *//*
	public String getParameter(String parameter) {
		String s = (String)this.parameters.get(parameter); 
		return (null == s) ? "" : s;
	}*/
	
/*	*//**
	 * 设置参数值
	 * @param parameter 参数名称
	 * @param parameterValue 参数值
	 *//*
	public void setParameter(String parameter, String parameterValue) {
		String v = "";
		if(null != parameterValue) {
			v = parameterValue.trim();
		}
		this.parameters.put(parameter, v);
	}*/
	
/*	*//**
	 * 返回所有的参数
	 * @return SortedMap
	 *//*
	public SortedMap getAllParameters() {
		return this.parameters;
	}*/
	
	/**
	 * 是否财付通签名,规则是:按参数名称a-z排序,遇到空值的参数不参加签名。
	 * @return boolean
	 */
	public boolean isTenpaySign(Map map) {
		
		//根据map,算出摘要
		String sign = MD5Util.md5Sign(map, WeiXinConfig.PARTNER_KEY);
		
	
		LOGGER.debug("微信回调参数拼接MD5加密str is:"+sign);
		//将字符串转成大写
		String tenpaySign = ((String)map.get("sign")).toUpperCase();
		LOGGER.debug("微信回调返回的签名tenpaySign:"+tenpaySign);
		System.out.println(tenpaySign + "=" + sign);
		boolean flag = tenpaySign.equals(sign);
		System.out.println(" flag is :" +flag);
		return flag;
	}
	
	/**
	 * 返回处理结果给财付通服务器。
	 * @param msg: Success or fail。
	 * @throws IOException 
	 */
	public void sendToCFT(String msg) throws IOException {
		String strHtml = msg;
		PrintWriter out = this.getHttpServletResponse().getWriter();
		out.println(strHtml);
		out.flush();
		out.close();

	}
	
	/**
	 * 获取uri编码
	 * @return String
	 */
	public String getUriEncoding() {
		return uriEncoding;
	}

/*	*//**
	 * 设置uri编码
	 * @param uriEncoding
	 * @throws UnsupportedEncodingException
	 *//*
	public void setUriEncoding(String uriEncoding)
			throws UnsupportedEncodingException {
		if (!"".equals(uriEncoding.trim())) {
			this.uriEncoding = uriEncoding;

			// 编码转换
			String enc = TenpayUtil.getCharacterEncoding(request, response);
			Iterator it = this.parameters.keySet().iterator();
			while (it.hasNext()) {
				String k = (String) it.next();
				String v = this.getParameter(k);
				v = new String(v.getBytes(uriEncoding.trim()), enc);
				this.setParameter(k, v);
			}
		}
	}*/

	/**
	*获取debug信息
	*/
	public String getDebugInfo() {
		return debugInfo;
	}
	
	/**
	*设置debug信息
	*/
	protected void setDebugInfo(String debugInfo) {
		this.debugInfo = debugInfo;
	}
	
	public HttpServletRequest getHttpServletRequest() {
		return this.request;
	}
	
	public HttpServletResponse getHttpServletResponse() {
		return this.response;
	}
	
}
